/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.asseco.mio.codex.controllers;

import com.asseco.mio.UserInstitution;
import com.asseco.mio.codex.Country;
import com.asseco.mio.codex.Institution;
import com.asseco.mio.codex.Municipality;
import com.asseco.mio.search.UserSearch;
import com.asseco.persistence.service.HibernateUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.appfuse.model.User;
import org.appfuse.service.UserManager;
import org.appfuse.service.impl.UserManagerImpl;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.asseco.bazi2.models.Katalog;
import com.asseco.bazi2.models.KatalogSifrarnik;
import com.asseco.bazi2.models.Klient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.asseco.mio.core.webapp.controller.BaseFormController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.asseco.mio.codex.controllers.*;
import org.springframework.validation.Validator;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import oracle.jdbc.OracleTypes;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class SimpleJsonGetController {

    private Map<Integer, Map<String, Integer>> cache = new HashMap<Integer, Map<String, Integer>>();

    @RequestMapping(method = {RequestMethod.GET})
    public ModelAndView handleRequest(
            @RequestParam(required = false, value = "parent") String parent,
            HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ClassNotFoundException, SQLException {


            parent = java.net.URLDecoder.decode(parent, "UTF-8");

            int parentInt = Integer.parseInt(parent);

            
      

            Configuration c = new Configuration().configure();
            SessionFactory sf = c.buildSessionFactory();
            Session session = sf.openSession();
            session.beginTransaction();
            Connection con = session.connection();  // obtain JDBC connection from Session object
            CallableStatement cs = con.prepareCall("{ call baza.SIFRARNIK_PACK.prebaruvanje_deca_roditel(?,?) }");
            cs.setInt(1, Integer.parseInt(parent));
            List<KatalogSifrarnik> resultList = new ArrayList<KatalogSifrarnik>();
            Map resultMap = new HashMap<String, Integer>();
            cs.registerOutParameter(2, OracleTypes.CURSOR);
            cs.execute();
            ResultSet resultSet = (ResultSet) cs.getObject(2);
            if (resultSet != null) {
                while (resultSet.next()) {
                    resultMap.put(resultSet.getString(1), resultSet.getInt(2));
                  
                }
                resultSet.close();
            }

            session.getTransaction().commit();
            session.close();
            sf.close();

            return new ModelAndView("jsonView", resultMap);
        }

    
}
